package com.dingding.transfer.stamper.util;

import com.dingding.transfer.stamper.dto.OperationRecord;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

/**
 * @author wangxiyuan
 * @description
 * @createDate 2021/3/30 18:34
 **/
public class GsonUtil {
    private static Gson filterNullGson;
    private static Gson nullableGson;
    static {
        nullableGson = new GsonBuilder()
                .enableComplexMapKeySerialization()
                .serializeNulls()
                .setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .registerTypeAdapter(Date.class, new DateAdapter())
                .create();
        filterNullGson = new GsonBuilder()
                .enableComplexMapKeySerialization()
                .setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .registerTypeAdapter(Date.class, new DateAdapter())
                .create();
    }

    protected GsonUtil() {
    }

    /**
     * 根据对象返回json   不过滤空值字段
     */
    public static String toJsonWtihNullField(Object obj){
        return nullableGson.toJson(obj);
    }

    /**
     * 根据对象返回json  过滤空值字段
     */
    public static String toJsonFilterNullField(Object obj){
        return filterNullGson.toJson(obj);
    }

    /**
     * 将json转化为对应的实体对象
     * new TypeToken<HashMap<String, Object>>(){}.getType()
     */
    public static <T>  T fromJson(String json, Type type){
        if(StringUtils.isBlank(json)){
            return null;
        }
        return nullableGson.fromJson(json, type);
    }

    /**
     * 将对象值赋值给目标对象
     * @param source 源对象
     * @param <T> 目标对象类型
     * @return 目标对象实例
     */
    public static <T> T convert(Object source, Class<T> clz){
        String json = GsonUtil.toJsonFilterNullField(source);
        return GsonUtil.fromJson(json, clz);
    }

    public static void main(String[] args) {
        /*String json="[" +
                "        {" +
                "            \"auto_add_user\": true," +
                "            \"create_dept_group\": true," +
                "            \"dept_id\": 37xxxx95," +
                "            \"name\": \"市场部\"," +
                "            \"parent_id\": 1" +
                "        }," +
                "        {" +
                "            \"auto_add_user\": true," +
                "            \"create_dept_group\": true," +
                "            \"dept_id\": 399xxxx96," +
                "            \"name\": \"财务部\"," +
                "            \"parent_id\": 1" +
                "        }" +
                "    ]";*/

       /* String json = "[" +
                "            {" +
                "                \"dept_order\": 1," +
                "                \"leader\": \"true\"," +
                "                \"extension\": \"{\\\"爱好\\\":\\\"旅游\\\",\\\"年龄\\\":\\\"24\\\"}\"," +
                "                \"unionid\": \"z21HjQlxxxx\"," +
                "                \"boss\": \"true\"," +
                "                \"exclusive_account\": \"false\"," +
                "                \"mobile\": \"18513027676\"," +
                "                \"active\": \"true\"," +
                "                \"admin\": \"true\"," +
                "                \"telephone\": \"010-86123456-2345\"," +
                "                \"remark\": \"备注备注\"," +
                "                \"avatar\": \"xxx\"," +
                "                \"hide_mobile\": \"false\"," +
                "                \"title\": \"技术总监\"," +
                "                \"hired_date\": \"1597573616828\"," +
                "                \"userid\": \"zhangsan\"," +
                "                \"work_place\": \"未来park\"," +
                "                \"org_email\": \"test@xxx.com\"," +
                "                \"name\": \"张三\"," +
                "                \"dept_id_list\": [" +
                "                    2," +
                "                    3," +
                "                    4" +
                "                ]," +
                "                \"job_number\": \"4\"," +
                "                \"state_code\": \"86\"," +
                "                \"email\": \"test@xxx.com\"" +
                "            }" +
                "        ]";
        List<UserInfo> userInfos = fromJson(json, new TypeToken<List<UserInfo>>() {
        }.getType());
        UserInfo userInfo = userInfos.get(0);
        System.out.println(userInfo.toString());
        User user = new User();
        BeanUtil.copyProperties(userInfo,user);
        System.out.println(user.toString());*/
        test();
    }
    
    public static void test(){
        String json = "[" +
                "            {" +
                "                \"date\": \"2021-04-01 09:37:05\", " +
                "                \"operation_result\": \"NONE\", " +
                "                \"operation_type\": \"START_PROCESS_INSTANCE\", " +
                "                \"userid\": \"manager9942\"" +
                "            }, " +
                "            {" +
                "                \"date\": \"2021-04-01 09:37:28\", " +
                "                \"operation_result\": \"AGREE\", " +
                "                \"operation_type\": \"EXECUTE_TASK_NORMAL\", " +
                "                \"remark\": \"我同意\", " +
                "                \"userid\": \"manager9942\"" +
                "            }, " +
                "            {" +
                "                \"date\": \"2021-04-01 09:38:46\", " +
                "                \"operation_result\": \"AGREE\", " +
                "                \"operation_type\": \"EXECUTE_TASK_NORMAL\", " +
                "                \"remark\": \"我再次同意\", " +
                "                \"userid\": \"manager9942\"" +
                "            }" +
                "        ]";
        List<OperationRecord> operationRecords = GsonUtil.fromJson(json, new TypeToken<List<OperationRecord>>() {
        }.getType());
        operationRecords.forEach(operationRecord -> {
            System.out.println( operationRecord.toString());
        });
    }
}
